package com.ly.user_system.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;


public class JvmLockService implements LockService {
    // 使用ConcurrentHashMap保存所有JVM锁实例
    private Lock lock=new ReentrantLock();


    @Override
    public <T> T tryLock(String lockKey, Supplier<T> supplier, long waitTime, TimeUnit timeUnit) {
        try {
            if (lock.tryLock(waitTime, timeUnit)) {
                    return supplier.get();
            }else {
                return null;
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            lock.unlock();
        }
    }


}
